package com.onelap.bls.treadmillcalibration.utils;


public class ScanRecordUtil {

    public static enum BroadcastPacketStatue {
        ConnectableUndirectedEventType,//可连接的非定向广播  0x00
        ConnectableDirectedEventType,//可连接的定向广播   0x01
        ScannableUndirectedEventType,// 可扫描的非定向广播 0x02
        NonConnectableUndirectedEventType,// 不可连接的非定向广播 0x03
        Other,  //0x04-0xFF
    }

    /**
     * 获取广播状态
     *
     * @param scanRecord
     * @return
     */
    public static BroadcastPacketStatue getBroadcastPacketStatue(byte[] scanRecord) {
        String strBroadcastPacket = getStrScanRecord(scanRecord);
        if (strBroadcastPacket.startsWith("03")) {
            return BroadcastPacketStatue.NonConnectableUndirectedEventType;
        } else return BroadcastPacketStatue.Other;
    }

    /**
     * 字符转化 bytesToHex
     *
     * @param bytes
     * @return
     */
    public static String getStrScanRecord(byte[] bytes) {
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }


    /**
     * 获取版本号
     *
     * @return
     */
    public static String getVersion(byte[] scanRecord) {
        String strBroadcastPacket = getStrScanRecord(scanRecord);
        String usefulData = strBroadcastPacket.substring(86, 106);//4252 3d6400cdff003a9e
        String strVersion = usefulData.substring(4, 6);
        int intVersion = Integer.parseInt(strVersion, 16);
        return String.valueOf(intVersion);
    }

    /**
     * 获取电量
     *
     * @return
     */
    public static String getPower(byte[] scanRecord) {
        String strBroadcastPacket = getStrScanRecord(scanRecord);
        String usefulData = strBroadcastPacket.substring(86, 106);//4252 3d6400cdff003a9e
        String strPower = usefulData.substring(6, 8);
        int intPower = Integer.parseInt(strPower, 16);
        return String.valueOf(intPower);
    }


}
